package com.graduate.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.graduate.common.entity.*;
import com.graduate.common.vo.HandleVo;
import com.graduate.common.vo.ReserveVo;
import com.sun.org.apache.regexp.internal.RE;
import com.sun.org.apache.xpath.internal.objects.XObject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author bwzfy
 * @create 2021/5/13
 **/
@Controller
@RequestMapping("/handle")
public class HandleController extends BaseController {

    /**
     * 根据传入的预定表的id来导入数据到办理表中
     *
     * @param id 预定表的id
     * @return
     */
    @ResponseBody
    @GetMapping("/import")
    public Result<String> importHandle(@RequestParam("id") String id) {
        // 首先查询出预定表的数据
        Reserve reserve = reserveService.getOne(new QueryWrapper<Reserve>().eq("mid", id).eq("status", "0"));
        if (reserve == null) {
            return new Result<String>().failure();
        }
        Handle handle = new Handle();
        BeanUtils.copyProperties(reserve, handle);
        // 把预定表信息复制到办理表中
        // 将办理表对象插入到预定表中
        boolean save = handleService.save(handle);
        // 将预定表中状态修改为已办理
        reserve.setStatus("1");
        UpdateWrapper<Reserve> wrapper = new UpdateWrapper<>();
        wrapper.set("status", reserve.getStatus()).eq("mid", reserve.getMid());
        reserveService.update(wrapper);
        if (save) {
            return new Result<String>().success();
        } else {
            return new Result<String>().failure();
        }
    }

    @ResponseBody
    @GetMapping("/list")
    public Result<IPage<HandleVo>> handleList(@RequestParam(required = false) Map<String, Object> map) {
        Result<IPage<HandleVo>> result = new Result<>();
        result.setCode(200);
        result.setData(handleService.selectHandlePage(map));
        return result;
    }

    // 进入到订单页面
    @GetMapping("/order/{id}")
    public String toOrder(@PathVariable("id") String id, Map<String, Object> map) {
        // 从数据库把房间信息查询出来，然后将数据封装进入request域中
        House house = houseService.getById(id);
        map.put("house", house);
        return "order-house";
    }

    @ResponseBody
    @GetMapping("/continue/{id}")
    public Result<String> continueLive(@PathVariable("id") String id) {
        // 把结束时间往后推一天
        Handle handle = handleService.getById(id);
        handle.getEndTime().setTime(handle.getEndTime().getTime() + 1000 * 60 * 60 * 24);
        UpdateWrapper<Handle> wrapper = new UpdateWrapper<>();
        wrapper.set("endTime", handle.getEndTime());
        wrapper.eq("mid", id);
        boolean update = handleService.update(wrapper);
        if (update) {
            return new Result<String>().success();
        }else {
            return new Result<String>().failure();
        }
    }

    /**
     * 根据传入的办理表的id结算订单，会在account表中存入数据，将导入表中的数据删除
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/account/{id}")
    public Result<String> account(@PathVariable("id") String id) {
        // 先从数据库中把办理表的数据查询出来
        Handle handle = handleService.getById(id);
        if (handle.getStatus() == 1) {
            Result<String> result = new Result<String>().failure();
            result.setData("不能重复结账");
            return result;
        }
        UpdateWrapper<Handle> wrapper = new UpdateWrapper<>();
        // 把办理表中的这条数据的状态设置为已导出
        wrapper.set("status", 1);
        wrapper.eq("mid", id);
        boolean update = handleService.update(wrapper);
        if (update) {
            // 创建account对象
            Account account = new Account();
            account.setIdCard(handle.getIdentificationNum());
            account.setRealName(handle.getRealName());
            account.setHid(handle.getHid());
            account.setStartTime(handle.getStartTime());
            account.setEndTime(handle.getEndTime());
            // 计算消费
            House house = houseService.getById(handle.getHid());
            double price = 0;
            if (handle.getMethod() == 0) {
                // 如果是0就是普通订房
                price = house.getHousePrice();
            } else if (handle.getMethod() == 1) {
                // ip订房
                price = house.getVipPrice();
            } else if (handle.getMethod() == 2) {
                // 组队购房
                price = house.getGroupPrice();
            }
            // 计算时长
            long l = handle.getEndTime().getTime() - handle.getStartTime().getTime();
            account.setCost(l / 1000 / 60 / 60 / 24 * price);
            accountService.save(account);
        }else {
            Result<String> result = new Result<String>().failure();
            result.setData("结账失败");
            return result;
        }
        return new Result<String>().success();
    }
}
